<?php
namespace app\tag\controller;
use think\Request;
use think\Cache;
use app\common\controller\Base;

class TagDetail extends Base
{
    protected $beforeActionList = ['start'];
    public function start() {
        $this->item = 'Tags';
        $this->itemType = 'tag';
        $this->itemName = '标签';
        $this->itemContent = 'TagsContent';
        $this->itemCategory = 'TagsCategory';
        $this->itemModelNameSpace = 'app\tag\model\Tags';
        $this->itemCategoryModelNameSpace = 'app\tag\model\TagsCategory';
    }
    public function index()
    {
        $id = input('param.id');
        $page = input('param.page');
        if (config('isTagName')) {
            $itemInfo = db('Tags')->where('name',$id)->find();
        } else {
            $itemInfo = db('Tags')->where('id',$id)->find();
            if ($itemInfo) {
                if ($itemInfo['url_name']) {
                    header('HTTP/1.1 301 Moved Permanently');
                    header('Location: ' . $this->domain .'/' . $this->settingInfo['tagModelUrl'] . '/' . $itemInfo['url_name'] .  '/');
                    exit();
                }
            } else {
                $itemInfo = db('Tags')->where('url_name',$id)->find();
            }
        }
        $itemInfo = model($this->itemModelNameSpace)->getItemInfo($itemInfo['id']);
        if (!$itemInfo) {
            throw new \think\exception\HttpException(404, '访问的内容不存在');
        }
        $this->assign('categoryUrlName',$itemInfo['categoryInfo']['url_name']);
        $this->assign('itemDetailId',$itemInfo['id']);
        $this->assign('page',$page ? $page : 1);
         
        $this->assign('itemInfo',$itemInfo);
        
        $templateName = $itemInfo['categoryInfo']['detail_template'] ? $itemInfo['categoryInfo']['detail_template'] : $this->itemType . 'Detail';
        $templateName = str_replace('.html', '', $templateName);
        
        return $this->siteView($this->itemType . '/' . $templateName);
    }


}